<template>
  <div class="permission-container">
    <div class="app-container">
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="addPermission({type:1,pid:'0'})">添加权限</el-button>
        </div>
        <!-- 表格 -->
        <el-table border :data="list" default-expand-all row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{row}">
              <el-button type="text" @click="addPermission({type:2,pid:row.id})">添加</el-button>
              <el-button type="text" @click="edit(row.id)">编辑</el-button>
              <el-button type="text" @click="del(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <permissionDialog ref="dialogRef" :dialog-visible="showDialog" :type="type" :pid="pid" @close="close" @update="getList" />
  </div>
</template>
<script>
import permissionDialog from './components/permission-dialog.vue'
import { getPermissionList, delPermission } from '@/api'
import { findChildren } from '@/utils'
export default {
  name: 'Permission',
  components: {
    permissionDialog
  },
  data() {
    return {
      list: [],
      showDialog: false,
      pid: '',
      type: 1
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      const res = await getPermissionList()
      console.log(res)
      this.list = findChildren(res.data, '0')
    },
    close() {
      this.showDialog = false
    },
    addPermission(obj) {
      this.showDialog = true
      this.type = obj.type
      this.pid = obj.pid
    },
    edit(id) {
      this.showDialog = true
      this.$refs.dialogRef.getDetail(id)
    },
    del(id) {
      // 1. 询问用户
      this.$confirm('此操作将永久删除该权限点, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 2. 调用删除接口
        await delPermission(id)
        // 3. 提示用户
        this.$message.success('删除权限点成功')
        // 4. 刷新列表
        this.getList()
      })
    }
  }

}
</script>

<style lang="scss" scoped>
.permission-container {
  padding: 30px;
}
</style>
